package com.chemical.search.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.chemical.search.dao.UserDAO;
import com.chemical.search.model.UserInfo;
import com.chemical.search.service.IUserManageService;
import com.chemical.search.util.page.Page;
import com.chemical.search.util.query.PropertyFilter;
import com.chemical.search.util.query.QueryUtil;

/**
 *@className:UserServiceImpl.java
 *@classDescription:
 *@author:MChen
 *@createTime:2012-4-20
 */
@Service
@SuppressWarnings("unchecked")
public class UserManageServiceImpl extends BaseService<UserInfo> implements IUserManageService{
	
	@Resource(name="userDAO")
	private UserDAO userDAO;
	
	/**
	 * 登录验证
	 */
	public UserInfo login(UserInfo user) {
		String hql="from UserInfo where user_name=:username and user_password=:password";
		Map map=new HashMap();
		map.put("username", user.getUser_name());
		map.put("password", user.getUser_password());
		return this.userDAO.findOfMap(hql, map);
	}
	
	/**
	 * 检测某用户名是否已存在
	 */
	public boolean checkUserName(String userName){
		boolean flag=true;
		String hql="from UserInfo where user_name=:userName";
		Map map=new HashMap();
		map.put("userName", userName);
		UserInfo user= this.userDAO.findOfMap(hql, map);
		if(null==user){
			flag=false;
		}
		return flag;
	}

	/**
	 * 显示所有用户(仅限于admin查看)
	 */
	public Page findAllUsers(Page page,List<PropertyFilter> pfList){
		//初始化hql
		StringBuffer hql=new StringBuffer("from UserInfo");
		//设置查询条件
		String condition= QueryUtil.toSqlString(pfList, true);
		hql.append(condition);
		//查找所有用户的总条数
		int totalCount=this.userDAO.findCountBySql(hql.toString());
		page.setTotalCount(totalCount);
		//是否存在排序
		if(page.isOrderBySetted()){
			hql.append(page.getOrder());
		}
		List<UserInfo> list=this.userDAO.findList(hql.toString(), page.getPageNo(), page.getPageSize());
		//查出结果集
		page.setResult(list);
		return page;
	}
}
